package com.itheima.edu.info.manager.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    private DateUtil() {
    }

    public static int getAgeByBirthday(String birthday) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = simpleDateFormat.parse(birthday);
            //获取到的是生日日期对应的毫秒值
            long birth = date1.getTime();

            //获取当前系统时间对应的毫秒值
            Date date2 = new Date();
            long now = date2.getTime();

            int age = (int) ((now - birth) / 1000L / 60 / 60 / 24 / 365);

            return age;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
